// pages/consultationRecord/index.js
import {
  ApiGetOrderList,
  ApiPatientCardList,
  ApiOrder
} from "../../api/docIndex.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: "all",
    name: '所有人',
    statusFormatter: [{
      label: '待支付',
      value: '1'
    }, {
      label: '进行中',
      value: '2'
    }, {
      label: '已完成',
      value: '3'
    }, {
      label: '已取消',
      value: '4'
    }, {
      label: '已评价',
      value: '5'
    }, {
      label: '退款中',
      value: '6'
    }],
    params: {
      size: 10,
      page: 1,
      finish: false,
    },

  },




  //进入问诊间
  /*   enterConsultationRoom(e) {
      let item = e.currentTarget.dataset.v
      const ops = {
        icon: item.icon,
        id: item.id,
        doctor_id: item.doctor_id,
        doctor_name: item.doctor_name,
        department_name: item.department_name,
        consultation_order_id: item.id,
        status: item.status,
        finish_time: item.finish_time,
        comment: item.comment,
        statusS: item.statusS,
        order_time: item.order_time,
        status: item.status,
        finish_time: item.finish_time,
        cancel_time: item.cancel_time
      }

      wx.setStorageSync('opsPatient', ops)
      wx.navigateTo({
        url: `/pagesDoctor/messageDetail/index`,
      })
    }, */


  enterConsultationRoom(e) {
    const item = e.currentTarget.dataset.v
    const ops = {
      icon: item.icon,
      id: item.id,
      doctor_id: item.patient_id,
      doctor_name: item.patient_name,
      department_name: item.department_name,
      consultation_order_id: item.id,
      status: item.status,
      finish_time: item.finish_time,
      comment: item.comment,
      statusS: item.statusS,
      order_time: item.order_time,
      status: item.status,
      finish_time: item.finish_time,
      cancel_time: item.cancel_time
    }

    wx.setStorageSync('messageDetail', ops)
    wx.navigateTo({
      url: `/pagesDoctor/messageDetail/index?contact=${JSON.stringify(ops)}`,
    })
  },



  checkStatus(item) {
    this.data.statusFormatter.forEach(statusItem => {
      if (item.status == statusItem.value) {
        item.statusLabel = statusItem.label
      }
    })
  },
  // 切换就诊人
  exChangeClick() {
    this.setData({
      showpopup: true
    })
  },

  onClose() {
    this.setData({
      showpopup: false
    })
  },


  //问诊记录详情
  recordDetailClick(e) {
    wx.navigateTo({
      url: `/pages/recordDetail/index?data=${JSON.stringify(e.currentTarget.dataset.v)}`,
    })
  },




  submitConsultation(e) {
    console.log(e)
    let that = this
    /*  this.setData({
       isLoading: true
     }) */
    wx.navigateTo({
      url: `/pages/submitConsultation/index?order=${JSON.stringify(e.currentTarget.dataset.v)}`,
    })
  },

  getChangeData(e) {
    if (e.detail.value == 'all') {
      this.getRecordList()
    } else {
      this.getRecordList(e.detail.value)
    }
  },
  getRecordList(id) {
    this.setData({
      showpopup: false
    })
    wx.showLoading({
      title: '加载中'
    })
    let recordArr = []
    ApiGetOrderList({
      ...this.data.params
    }).then(res => {
      if (res.code == 200) {
        const data = res.data
        res.data.forEach(item => {
          this.checkStatus(item)
        })
        recordArr = this.data.params.page === 1 ? data : this.data.recordArr.concat(data)
        this.data.params.finish = recordArr.length >= res.page.total
        this.setData({
          recordArr,
          loading: false
        })
        wx.hideLoading()
      }
    })
  },

  onLoadMore() {
    if (this.data.loading || this.data.params.finish) return
    this.data.params.page += 1
    this.getRecordList(this.data.id)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  onShow() {
    this.getRecordList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */



  //获取就诊人列表
  /*   getPatientList() {
      ApiPatientCardList().then(res => {
        this.setData({
          patientArr: res.data
        })
      })
    }, */



  //点击
  /*   selectPatient(e) {
      console.log(e)
      let value = e.currentTarget.dataset
      if (value.v == '所有人') {
        console.log('1')
        this.setData({
          active: 'all',
          name: '所有人',
        })
        this.getRecordList()
      } else {
        this.setData({
          active: value.info,
          name: value.v.name
        })
        this.getRecordList(value.v.id)
      }

    }, */
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})